// 蚁群拼接.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。

#include "pheromone_map.h"
#include "utils.h"
#include "ant.h"
#include<vector>
#include <iostream>
#include <stdlib.h>
#include <cstdlib>
#include <time.h>
#include<queue>
#include<functional>
#include<algorithm>
#include<ctime>
#include "ant_colony.h"
#include "compose.h"

using namespace std;
vector<polygon>compose::allPolygon;
vector<rectangle>compose::allRectangle;
vector<float> area_rate_vec;
int main(int argc, char* argv[])
{
    //比赛用写法
//    name = argv[1];
//    output_filename = resolution_path(name);

    //本地测试写法
    name = "E:\\EDA\\test\\polygon_area_etc_input_5.txt";
    output_filename="E:\\EDA\\output_5.txt";

    int help = 0;
    vector<rectangle*> r;
    polygon PG[1500];
    vector<string> s;
    vector<string> s2;
    s = Read(help);
    filter(s,s2);
    vector<int> v;
    vector<int> c;
    String_to_data(s2, v, c, help);

    Getmodel(compose::allRectangle, v, c, compose::allPolygon, PG);
    int rec_count=0;
    int poly_count=0;
    for(int i=0;i<c.size();++i)
    {
       if(c[i]==8)
       {
          compose::allRectangle[rec_count].init_inf=s[2*i+1];
          rec_count++;
       }
       else
       {
          compose::allPolygon[poly_count].init_inf=s[2*i+1];
          poly_count++;
       }
    }
    for(const polygon& poly:compose::allPolygon){
          if(judge_if_concave(poly)){
              r.push_back(new Concave(poly));
          }
      }
      for(rectangle* rec:r){
          for(auto it=compose::allPolygon.begin();it!=compose::allPolygon.end();++it){
              if(it->init_inf==rec->init_inf){
                  compose::allPolygon.erase(it);
                  break;
              }
          }
      }



    vector<Assembled_rec> temp_asb_rec = compose::testCompose();
    compose::insert_rectangles(temp_asb_rec);
    int area_sum = 0;
    int area_occu = 0;
    for (int i = 0; i < temp_asb_rec.size(); i++) {
        area_sum += temp_asb_rec[i].area;

        area_occu += temp_asb_rec[i].get_occu_area();
    }
    cout << "ZT rate:" << ((double)area_occu / (double)area_sum) << endl;
    int asb_rec_area_sum = 0;
    int rec_area_sum = 0;
    for (int i = 0; i < temp_asb_rec.size(); i++) {
        r.push_back(&temp_asb_rec[i]);
        asb_rec_area_sum += temp_asb_rec[i].get_occu_area();
    }
    int asb_rec_size = temp_asb_rec.size();
    for (int i = 0; i < compose::allRectangle.size(); i++) {
        r.push_back(&compose::allRectangle[i]);
        rec_area_sum += compose::allRectangle[i].get_occu_area();
    }
    cout << "wei rectangle sum_area:" << asb_rec_area_sum;
    cout << "rectangle sum_area:" << rec_area_sum;




    AddTurn(r);
    for (int i = 0; i < r.size(); i++)
    {
        r[i]->index = i;
    }

    int area=9999999;//记录当前最小值
    int final_area;
    //先验蚁群
    int area_com_sum;
    float pre_sum=9999999;
    for(int i=0;i<100;i++)//多轮先验蚁群
    {
        ant_colony ant_clolony_test(r, 10);
        ant_clolony_test.ants_run(true,r);
        area_com_sum=ant_clolony_test.ants[0]->area;
        float area_temp=ant_clolony_test.find_min_area();
        if(pre_sum>area_temp)
            pre_sum=area_temp;
    }
    cout  << "pre_rate" << (double)area_com_sum /pre_sum;
    //print_map(36);
    for(int round=0;round<90;round++)
    {
        ant_colony ant_clolony_one(r, 10);
        area_com_sum=ant_clolony_one.ants[0]->area;

        ant_clolony_one.ants_run(false,r);
        ant_clolony_one.find_min_area();

        vector<rectangle*> V;
        for (int j = 0; j < ant_clolony_one.ants[ant_clolony_one.best_ant_index]->finish_index.size(); j++)
        {
            int index = ant_clolony_one.ants[ant_clolony_one.best_ant_index]->finish_index[j];
            V.push_back(ant_clolony_one.ants[ant_clolony_one.best_ant_index]->rt_v[index]);
        }
        float area_rate=(double)area_com_sum/get_area_sum(V);//本轮最优利用率
        area_rate_vec.push_back(area_rate);//将本轮最优利用率压入vector


        //如果得到了比上一轮更优解
        if(area>get_area_sum(V))
        {
            area=get_area_sum(V);
            final_area=area;
            fstream op;
            op.open(output_filename, ios::out);                //打开文件路径
            for (int i = 0; i < V.size(); i++)
            {
                V[i]->write_file(op);
            }
            op.close();
        }
    }
    cout  << "final_rate" << (double)area_com_sum / final_area << endl;
    cout<<"area rate vector:";
    for(int i=0;i<area_rate_vec.size();i++)
    {
        cout<<area_rate_vec[i]<<endl;
    }
    write_vec(area_rate_vec);//面积利用率变化写入文件

}



